Python Basic Exercise A

  • v1.1, 2020.4 edit by David Yi

题目1:两个正整数a和b, 输出它们的最大公约数。

例如:a = 24, b = 16

则输出:8


In [2]:
a = 24
b = 16
for i in range(min(a,b), 0, -1): 
    if a % i == 0 and b % i ==0: 
        print(i) 
        break


8

In [3]:
while b:
    a,b=b,a%b
print(a)


8

In [4]:
print(max([x for x in range(1,a+1) if a%x==0 and b%x==0]))


8

题目2:输出 9*9 乘法口诀表

程序分析:分行与列考虑,共9行9列,i控制行,j控制列。


In [11]:
for i in range(1, 10):
    for j in range(1, i+1):
        print ("%d x %d = %d" % (i, j, i*j),"\t",end="")
        if i==j:
            print("")


1 x 1 = 1 	
2 x 1 = 2 	2 x 2 = 4 	
3 x 1 = 3 	3 x 2 = 6 	3 x 3 = 9 	
4 x 1 = 4 	4 x 2 = 8 	4 x 3 = 12 	4 x 4 = 16 	
5 x 1 = 5 	5 x 2 = 10 	5 x 3 = 15 	5 x 4 = 20 	5 x 5 = 25 	
6 x 1 = 6 	6 x 2 = 12 	6 x 3 = 18 	6 x 4 = 24 	6 x 5 = 30 	6 x 6 = 36 	
7 x 1 = 7 	7 x 2 = 14 	7 x 3 = 21 	7 x 4 = 28 	7 x 5 = 35 	7 x 6 = 42 	7 x 7 = 49 	
8 x 1 = 8 	8 x 2 = 16 	8 x 3 = 24 	8 x 4 = 32 	8 x 5 = 40 	8 x 6 = 48 	8 x 7 = 56 	8 x 8 = 64 	
9 x 1 = 9 	9 x 2 = 18 	9 x 3 = 27 	9 x 4 = 36 	9 x 5 = 45 	9 x 6 = 54 	9 x 7 = 63 	9 x 8 = 72 	9 x 9 = 81 	

In [6]:
i=0
j=0
while i<9:
    i+=1
    while j<9:
        j+=1
        print(j,"x",i,"=",i*j,"\t",end="")
        if i==j:
            j=0
            print("")
            break


1 x 1 = 1 	
1 x 2 = 2 	2 x 2 = 4 	
1 x 3 = 3 	2 x 3 = 6 	3 x 3 = 9 	
1 x 4 = 4 	2 x 4 = 8 	3 x 4 = 12 	4 x 4 = 16 	
1 x 5 = 5 	2 x 5 = 10 	3 x 5 = 15 	4 x 5 = 20 	5 x 5 = 25 	
1 x 6 = 6 	2 x 6 = 12 	3 x 6 = 18 	4 x 6 = 24 	5 x 6 = 30 	6 x 6 = 36 	
1 x 7 = 7 	2 x 7 = 14 	3 x 7 = 21 	4 x 7 = 28 	5 x 7 = 35 	6 x 7 = 42 	7 x 7 = 49 	
1 x 8 = 8 	2 x 8 = 16 	3 x 8 = 24 	4 x 8 = 32 	5 x 8 = 40 	6 x 8 = 48 	7 x 8 = 56 	8 x 8 = 64 	
1 x 9 = 9 	2 x 9 = 18 	3 x 9 = 27 	4 x 9 = 36 	5 x 9 = 45 	6 x 9 = 54 	7 x 9 = 63 	8 x 9 = 72 	9 x 9 = 81 	

In [12]:
print('\n'.join(['\t'.join(["%2s x%2s = %2s"%(j,i,i*j) for j in range(1,i+1)]) for i in range(1,10)]))


 1 x 1 =  1
 1 x 2 =  2	 2 x 2 =  4
 1 x 3 =  3	 2 x 3 =  6	 3 x 3 =  9
 1 x 4 =  4	 2 x 4 =  8	 3 x 4 = 12	 4 x 4 = 16
 1 x 5 =  5	 2 x 5 = 10	 3 x 5 = 15	 4 x 5 = 20	 5 x 5 = 25
 1 x 6 =  6	 2 x 6 = 12	 3 x 6 = 18	 4 x 6 = 24	 5 x 6 = 30	 6 x 6 = 36
 1 x 7 =  7	 2 x 7 = 14	 3 x 7 = 21	 4 x 7 = 28	 5 x 7 = 35	 6 x 7 = 42	 7 x 7 = 49
 1 x 8 =  8	 2 x 8 = 16	 3 x 8 = 24	 4 x 8 = 32	 5 x 8 = 40	 6 x 8 = 48	 7 x 8 = 56	 8 x 8 = 64
 1 x 9 =  9	 2 x 9 = 18	 3 x 9 = 27	 4 x 9 = 36	 5 x 9 = 45	 6 x 9 = 54	 7 x 9 = 63	 8 x 9 = 72	 9 x 9 = 81

题目3:编写一个函数,给你三个整数a,b,c, 判断能否以它们为三个边长构成三角形。若能,输出YES,否则输出NO


In [5]:
def func1(a,b,c):
    if (a+b)>c and (b+c)>a and (a+c)>b:
        return 'yes'
    else:
        return 'no'


print(func1(1,2,3))


no

In [3]:
def func2(a,b,c):
    if max(a,b,c)<(a+b+c)/2.0:
        return 'yes'
    else:
        return 'no'

print(func2(3,4,5))


yes

题目4,简单的四则计算器


In [17]:
# 简单四则计算器代码,摘自 https://www.jianshu.com/p/c22bfd91df49

def calculate():
     operation = input('''
     please type in the math operation you would like to complete:
     + for addition
     - for subtraction
     * for multiplication
     / for division
     ''')
     number_1 = float(input('Please enter the first number: '))
     number_2 = float(input('Please enter the second number: '))
     if operation == '+': 
          print('{} + {} = {} '.format(number_1, number_2,number_1 + number_2))          
                                    
     elif operation == '-':
          print('{} - {} = {} '.format(number_1,number_2,number_1 - number_2))
     elif operation == '*':                                      
          print('{} * {} = {} '.format(number_1, number_2,number_1 * number_2))
     elif operation == '/' and number_2 != 0:                                      
          print('{} / {} = {} '.format(number_1, number_2,number_1 / number_2))
     else:
          print('You have not typed a valid operator, please run the program again.')


calculate()


     please type in the math operation you would like to complete:
     + for addition
     - for subtraction
     * for multiplication
     / for division
     +
Please enter the first number: 1
Please enter the second number: 4
1.0 + 4.0 = 5.0